//
//  PYCancelFollowView.m
//  catuncel
//
//  Created by Jim on 2017/12/14.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "PYCancelFollowView.h"

@interface PYCancelFollowView ()

@property (nonatomic, strong) UIVisualEffectView *upBackView;

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) UILabel *title;

@property (nonatomic, strong) UIView *infoView;

@property (nonatomic, strong) UIButton *stopFollowBtn;

@property (nonatomic, strong) UIVisualEffectView *downBackView;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) UIView *shadowView;

@property (nonatomic, copy) PYCancelFollowBlock block;

@end

@implementation PYCancelFollowView

- (UIImageView *)imgView
{
    if (!_imgView) {
        
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 60) / 2, 26.5, 30, 30)];
    
        _imgView.layer.cornerRadius = 14;
        
        _imgView.layer.masksToBounds = YES;
    }
    
    return _imgView;
}

- (UILabel *)title
{
    if (!_title) {
        
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 71, SCREEN_WIDTH - 30, 12)];
        
        _title.textColor = [UIColor colorWithWhite:34.f/255.f alpha:1];
    
        _title.textAlignment = NSTextAlignmentCenter;
        
        _title.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    }
    
    return _title;
}

- (UIButton *)stopFollowBtn
{
    if (!_stopFollowBtn) {
        
        _stopFollowBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH - 30, 55)];
        
        _stopFollowBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
        [_stopFollowBtn setTitle:@"不再关注" forState:UIControlStateNormal];
        
        [_stopFollowBtn setTitleColor:[UIColor colorWithRed:237.f/255.f green:73.f/255.f blue:86.f/255.f alpha:1] forState:UIControlStateNormal];
        
        _stopFollowBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        
        [_stopFollowBtn addTarget:self action:@selector(stopFollowBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_stopFollowBtn addTarget:self action:@selector(stopFollowBtnTouchDownAction) forControlEvents:UIControlEventTouchDown];
        
        [_stopFollowBtn addTarget:self action:@selector(stopFollowBtnTouchDragExitAction) forControlEvents:UIControlEventTouchDragExit];
        
        [_stopFollowBtn addTarget:self action:@selector(stopFollowBtnTouchCancelAction) forControlEvents:UIControlEventTouchCancel];
        
        [_stopFollowBtn addTarget:self action:@selector(stopFollowBtnTouchDragEnterAction) forControlEvents:UIControlEventTouchDragEnter];

    }
    
    return _stopFollowBtn;
}

- (UIView *)infoView
{
    if (!_infoView) {
        
        _infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, 99)];
        
        _infoView.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
        [_infoView addSubview:self.imgView];
        
        [_infoView addSubview:self.title];
    }
    
    return _infoView;
}

- (UIVisualEffectView *)upBackView
{
    if (!_upBackView) {
        
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        _upBackView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        
        _upBackView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 30, 155);
        
        _upBackView.layer.cornerRadius = 14;
        
        _upBackView.layer.masksToBounds = YES;
        
        [_upBackView.contentView addSubview:self.infoView];
        
        [_upBackView.contentView addSubview:self.stopFollowBtn];
    }
    
    return _upBackView;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        
        _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, 60)];

        _cancelBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        
        [_cancelBtn setTitleColor:[UIColor colorWithRed:0 green:0.48 blue:1 alpha:1] forState:UIControlStateNormal];
        
        _cancelBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        
        [_cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_cancelBtn addTarget:self action:@selector(cancelBtnTouchDownAction) forControlEvents:UIControlEventTouchDown];
        
        [_cancelBtn addTarget:self action:@selector(cancelBtnTouchDragExitAction) forControlEvents:UIControlEventTouchDragExit];
        
        [_cancelBtn addTarget:self action:@selector(cancelBtnTouchCancelAction) forControlEvents:UIControlEventTouchCancel];
        
        [_cancelBtn addTarget:self action:@selector(cancelBtnTouchDragEnterAction) forControlEvents:UIControlEventTouchDragEnter];
    }
    
    return _cancelBtn;
}

- (UIVisualEffectView *)downBackView
{
    if (!_downBackView) {
        
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        _downBackView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        
        _downBackView.frame = CGRectMake(0, 165, SCREEN_WIDTH - 30, 60);
        
        _downBackView.layer.masksToBounds = YES;
        
        _downBackView.layer.cornerRadius = 14;
        
        [_downBackView.contentView addSubview:self.cancelBtn];
    }
    
    return _downBackView;
}

- (UIView *)backView
{
    if (!_backView) {
        
        _backView = [[UIView alloc] initWithFrame:CGRectMake(15, SCREEN_HEIGHT, SCREEN_WIDTH - 30, 220)];
        
        [_backView addSubview:self.upBackView];
        
        [_backView addSubview:self.downBackView];
    
    }
    
    return _backView;
}

- (UIView *)shadowView
{
    if (!_shadowView) {
        
        _shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
        _shadowView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelBtnAction)];
        
        [_shadowView addGestureRecognizer:tap];
    }
    
    return _shadowView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.shadowView];

        [self addSubview:self.backView];
        
    }
    return self;
}

- (void)stopFollowBtnAction
{
    [self cancelBtnAction];
    
    if (self.block) {
        
        self.block();
    }
    
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.stopFollowBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];

    }];
}

- (void)stopFollowBtnTouchDownAction
{
    self.stopFollowBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.3];
}

- (void)stopFollowBtnTouchCancelAction
{
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.stopFollowBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
    }];
    
}

- (void)stopFollowBtnTouchDragExitAction
{
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.stopFollowBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
    }];
}

- (void)stopFollowBtnTouchDragEnterAction
{
    self.stopFollowBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
}

- (void)cancelBtnAction
{
    [UIView animateWithDuration:PY_Animation_Duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        self.backView.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, 245);
        
        self.shadowView.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        if (finished) {
            
            [self removeFromSuperview];
        }
    }];
    
    
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.cancelBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
    }];
}

- (void)cancelBtnTouchDownAction
{
    self.cancelBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.3];
}

- (void)cancelBtnTouchCancelAction
{
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.cancelBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
    }];
}

- (void)cancelBtnTouchDragExitAction
{
    [UIView animateWithDuration:PY_Animation_Duration animations:^{
        
        self.cancelBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
        
    }];
}

- (void)cancelBtnTouchDragEnterAction
{
    self.cancelBtn.backgroundColor = [UIColor colorWithWhite:0.97 alpha:0.8];
}

+ (void)showWithImageUrl:(NSString *)imgUrl name:(NSString *)name block:(PYCancelFollowBlock)block
{
    PYCancelFollowView *cancelFollowView = [[PYCancelFollowView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    [cancelFollowView.imgView py_setImageWithURL:[imgUrl qiniuImageUrlWithWidth:50]];
    
    if ([name isValidString]) {
        
        NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"不再关注 %@", name]];
        
        [attriStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(5, name.length)];
        
        cancelFollowView.title.attributedText = attriStr;
        
    }
    
    cancelFollowView.block = [block copy];
    
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    
    //设置运动时间
    animation.duration = PY_Animation_Duration;
    
    //设置运动type
    animation.type = kCATransitionFade;
    
    //设置运动速度
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
    
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];
    
    [[UIApplication sharedApplication].keyWindow addSubview:cancelFollowView];
    
    [UIView animateWithDuration:PY_Animation_Duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        cancelFollowView.backView.transform = CGAffineTransformMakeTranslation(0, -245);
        
    } completion:nil];
}

@end
